package online.heycm.codegenerator.common.session;

import lombok.Data;
import online.heycm.codegenerator.common.types.DBType;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author hey
 * @version 1.0
 * @date 2023/12/21 10:13
 */
@Data
public class Session {

    private final String token;

    private final Connection connection;

    private final long createdAt;

    private long operateAt;

    private DBType dbType;

    private String url;

    private String username;

    private String password;

    private String output;

    public Session(String token, Connection connection) {
        this.token = token;
        this.connection = connection;
        this.createdAt = System.currentTimeMillis();
        this.operateAt = this.createdAt;
    }

    public void close() {
        SessionStorage.close(this);
    }

    public boolean isAlive() {
        try {
            return connection != null && !connection.isClosed();
        } catch (SQLException ignored) {

        }
        return false;
    }

}
